<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container>
      <a-form-model ref="form" :model="model" slot="detail" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="旧密码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="oldpassword">
              <a-input-password
                v-model="model.oldpassword"
                type="password"
                placeholder="请输入"
                visibilityToggle
              ></a-input-password>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="新密码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="password">
              <a-input-password
                v-model="model.password"
                type="password"
                placeholder="请输入新密码（6至12位数字、字母或符号）"
                visibilityToggle
              ></a-input-password>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="确认新密码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="confirmpassword">
              <a-input-password
                v-model="model.confirmpassword"
                type="password"
                placeholder="请再次输入新密码"
                visibilityToggle
              ></a-input-password>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'

export default {
  name: 'TenantForm',
  components: {
    JFormContainer,
    JDate,
    JDictSelectTag
  },
  props: {
    formData: {
      type: Object,
      default: () => {},
      required: false
    },
    normal: {
      type: Boolean,
      default: false,
      required: false
    },
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      // info: {},
      model: {
        oldpassword: '',
        password: '',
        confirmpassword: ''
      },
      id: '',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        confirmpassword: [{ required: true, validator: this.validatePasswordC }],
        oldpassword: [{ required: true, validator: this.validatePasswordOld }],
        password: [{ required: true, validator: this.validatePassword }]
      },
      url: {
        updateUrl: '/sys/user/changePassword'
      }
    }
  },
  computed: {},
  created() {},
  methods: {
    show(info) {
      // this.info = info
      // console.log(this.info, 'this.info')
    },
    // 校验密码输入
    validatePassword(rule, value, callback) {
      if (!value) {
        callback('请输入新密码（6至12位数字、字母或符号）!')
        return
      }
      if(new RegExp(/[\u4e00-\u9fa5]/).test(value)){
        callback('密码为6至12位的字母、数字、符号或任意组合!')
      } 
      if (new RegExp(/^[a-zA-Z0-9\W]{6,12}$/).test(value)) {
        callback()
      }else {
        callback('密码为6至12位的字母、数字、符号或任意组合!')
      }
    },
    validatePasswordC(rule, value, callback) {
      if (!value) {
        callback('请再次输入新密码!')
        return
      }
      if(new RegExp(/[\u4e00-\u9fa5]/).test(value)){
        callback('密码为6至12位的字母、数字、符号或任意组合!')
      } 
      if (new RegExp(/^[a-zA-Z0-9\W]{6,12}$/).test(value)) {
        callback()
      }else {
        callback('密码为6至12位的字母、数字、符号或任意组合!')
      }
    },
    validatePasswordOld(rule, value, callback) {
      if (!value) {
        callback('请输入!')
        return
      }
      if(new RegExp(/[\u4e00-\u9fa5]/).test(value)){
        callback('密码为6至12位的字母、数字、符号或任意组合!')
      } 
      if (new RegExp(/^[a-zA-Z0-9\W]{6,12}$/).test(value)) {
        callback()
      }else {
        callback('密码为6至12位的字母、数字、符号或任意组合!')
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      that.$refs.form.validate(valid => {
        if (valid) {
          let httpurl = that.url.updateUrl
          let method = 'put'
          let param = that.model
          // param.userSids = that.info.sid
          // param.username = that.info.username
          // console.log(param, 'param')
          if(param.password !== param.confirmpassword){
            this.$antdMessage.error('新密码与确认新密码不一致')
            return
          }
          that.confirmLoading = true
          httpAction(httpurl, that.model, method)
            .then(res => {
              if (res.success) {
                that.$antdMessage.success(res.message)
                that.$emit('ok')
              } else {
                that.$antdMessage.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        } else {
          return false
        }
      })
    },
    popupCallback(row) {
      this.model = Object.assign(this.model, row)
    }
  }
}
</script>
